----------------------------------------------------------
--	Name: 		Alt Info Revised Script	                --
--  Copyright 2012 Mertsch								--
----------------------------------------------------------

local _G = getfenv(0)
local ipairs, pairs, select, string, table, next, type, unpack, tinsert, tconcat, tremove, fmt, tostring, tonumber, tsort = _G.ipairs, _G.pairs, _G.select, _G.string, _G.table, _G.next, _G.type, _G.unpack, _G.table.insert, _G.table.concat, _G.table.remove, _G.string.format, _G.tostring, _G.tonumber, _G.table.sort
local interface = object

local Game = {}
Game.ABILITIES_START		= 0
Game.ABILITIES_END			= 4
Game.INVENTORY_START        = 48
Game.INVENTORY_END          = 53

local AltInfoRevisedGamePanel = nil

LHA = {}
LHA.BonusDamage = 0

local function GetArmorFactor(armor)
	if (armor < 0) then
		return 1.0 / (2.0 - math.pow(0.94, math.abs(armor)))
	else
		return 1.0 / (1.0 - ((0.06 * armor) / (1.0 + (0.06 * armor))))
	end
end

local function GetEffectiveDamage()
	local EffectiveDamage, EffectiveDamageEnemy
	if (LHA.HasHatchet) then
		if (LHA.IsMelee) then
			EffectiveDamage = LHA.ActiveDamage * 1.32
		else
			EffectiveDamage = LHA.ActiveDamage * 1.12
		end
	else
		EffectiveDamage = LHA.ActiveDamage
	end

	EffectiveDamageEnemy = EffectiveDamage + LHA.BonusDamage

	if (LHA.EffectiveDamage ~= EffectiveDamage or LHA.EffectiveDamageEnemy ~= EffectiveDamageEnemy) then
		LHA.EffectiveDamage = EffectiveDamage
		LHA.EffectiveDamageEnemy = EffectiveDamageEnemy
		println('^gLHA.EffectiveDamage='..tostring(LHA.EffectiveDamage)..', LHA.EffectiveDamageEnemy='..tostring(LHA.EffectiveDamageEnemy))
		Trigger('LHAUpdated')
	end
end

local function CheckItems()
	local HasHatchet, IsHatchetActive = false, false

	for i,exists in ipairs(LHA.InventoryItemExists) do
		if (exists) then
			if (LHA.InventoryItemNames[i] == 'Logger\'s Hatchet') then
				HasHatchet = true
				if (LHA.InventoryItemIsActiveSlot[i]) then
					IsHatchetActive = true
				end
			end
		end
	end

	if (IsHatchetActive) then
		HasHatchet = false
		--println('^555Debug: Hatchet is active!')
	end

	if (LHA.HasHatchet ~= HasHatchet) then
		LHA.HasHatchet = HasHatchet
		println('^gLHA.HasHatchet='..tostring(LHA.HasHatchet))
		GetEffectiveDamage()
	end
end

local function InitActiveTarget()
	println('^555Debug: game_air.lua:InitActiveTarget() BEGIN')

	LHA.ActiveDamage = 0
	LHA.IsMelee = false
	
	local function ActiveDamage(sourceWidget, minAtkDmg, maxAtkDmg, avgAtkDmg)
		local ActiveDamage = AtoN(minAtkDmg) + AtoN(avgAtkDmg)
		if (LHA.ActiveDamage ~= ActiveDamage) then
			LHA.ActiveDamage = ActiveDamage
			println('^gLHA.ActiveDamage=' .. tostring(LHA.ActiveDamage))
			GetEffectiveDamage()
		end
	end
	AltInfoRevisedGamePanel:RegisterWatch('ActiveDamage', ActiveDamage)

	local function ActiveAttackInfo(self, activeAttack)
		local IsMelee = tostring(activeAttack) == 'Melee'
		if (LHA.IsMelee ~= IsMelee) then
			LHA.IsMelee = IsMelee
			println('^gLHA.IsMelee=' .. tostring(LHA.IsMelee))
			GetEffectiveDamage()
		end
	end
    AltInfoRevisedGamePanel:RegisterWatch('ActiveAttackInfo', ActiveAttackInfo)

	LHA.InventoryItemExists = {}
	LHA.InventoryItemNames = {}
	LHA.InventoryItemIsActiveSlot = {}

	local function ActiveInventoryExists(i, sourceWidget, exists)
		local exists = AtoB(exists)
		if (LHA.InventoryItemExists[i] ~= exists) then
			LHA.InventoryItemExists[i] = exists
			--println('^555Debug: LHA.InventoryItemExists['..tostring(i)..']='..tostring(exists))
			CheckItems()
		end
	end
	local function ActiveInventoryDescription(i, sourceWidget, description)
		local description = tostring(description)
		if (LHA.InventoryItemNames[i] ~= description) then
			LHA.InventoryItemNames[i] = description
			--println('^555Debug: LHA.InventoryItemNames['..tostring(i)..']='..tostring(description))
			CheckItems()
		end
	end
	local function ActiveInventoryStatus(slotIndex, sourceWidget, canActivate, isActive, isDisabled, needMana, inUse, currentLevel, canLevelUp, maxLevel, isActiveSlot, canShare, isBorrowed, team, recentPurchase, index, iSlot)
		--println('^555Debug: game_air.lua:ActiveInventoryStatus(isActive = '..tostring(isActive)..', isActiveSlot = '..tostring(isActiveSlot)..', inUse = '..tostring(inUse)..')')
		local isActiveSlot = AtoB(isActiveSlot)
		if (LHA.InventoryItemIsActiveSlot[slotIndex] ~= isActiveSlot) then
			LHA.InventoryItemIsActiveSlot[slotIndex] = isActiveSlot
			--println('^555Debug: LHA.InventoryItemIsActiveSlot['..tostring(slotIndex)..']='..tostring(isActiveSlot))
			CheckItems()
		end
	end

	for inventoryIndex = Game.INVENTORY_START, Game.INVENTORY_END, 1 do
		local i = 1 + (inventoryIndex - Game.INVENTORY_START)
		LHA.InventoryItemExists[i] = false
		LHA.InventoryItemNames[i] = nil
		LHA.InventoryItemIsActiveSlot[i] = false
		AltInfoRevisedGamePanel:RegisterWatch('ActiveInventoryExists'     ..tostring(inventoryIndex), function(...) ActiveInventoryExists(i, ...)      end)
		AltInfoRevisedGamePanel:RegisterWatch('ActiveInventoryDescription'..tostring(inventoryIndex), function(...) ActiveInventoryDescription(i, ...) end)
		AltInfoRevisedGamePanel:RegisterWatch('ActiveInventoryStatus'     ..tostring(inventoryIndex), function(...) ActiveInventoryStatus(i, ...)      end)
	end

	LHA.Abilities = {}
	
	local function CheckAbilities()
		local BonusDamage = 0

		if (LHA.Abilities[3].Description == 'Horned Strike') then
			if (LHA.Abilities[3].CurrentLevel > 0) then
				if (LHA.Abilities[3].CanActivate) then
					BonusDamage = 40 + (LHA.Abilities[3].CurrentLevel * 20)
				end
			end
		end

		if (LHA.BonusDamage ~= BonusDamage) then
			LHA.BonusDamage = BonusDamage
			println('^gLHA.BonusDamage='..tostring(LHA.BonusDamage))
			GetEffectiveDamage()
		end
	end
	local function ActiveAbilityDescription(slotIndex, sourceWidget, description)
		if (slotIndex == 3) then
			local description = tostring(description)
			if (LHA.Abilities[slotIndex].Description ~= description) then
				LHA.Abilities[slotIndex].Description = description
				--println('^555Debug: LHA.LHA.Abilities['..tostring(slotIndex)..'].Description='..tostring(description))
				CheckAbilities()
			end
		end
	end
	local function ActiveAbilityStatus(slotIndex, sourceWidget, canActivate, isActive, isDisabled, needMana, inUse, currentLevel, canLevelUp, maxLevel, isActiveSlot, canShare, isBorrowed, team, recentPurchase, index, iSlot)
		--println('^555Debug: game_air.lua:ActiveAbilityStatus(slotIndex='..tostring(slotIndex)..', canActivate='..tostring(canActivate)..', isActive='..tostring(isActive)..', inUse='..tostring(inUse)..', currentLevel='..tostring(currentLevel)..', isActiveSlot='..tostring(isActiveSlot)..')')
		if (slotIndex == 3) then
			local canActivate = AtoB(canActivate)
			local currentLevel = AtoN(currentLevel)
			if (LHA.Abilities[slotIndex].CanActivate ~= canActivate or LHA.Abilities[slotIndex].CurrentLevel ~= currentLevel) then
				LHA.Abilities[slotIndex].CanActivate = canActivate
				LHA.Abilities[slotIndex].CurrentLevel = currentLevel
				--println('^555Debug: LHA.Abilities['..tostring(slotIndex)..'].CanActivate='..tostring(canActivate))
				--println('^555Debug: LHA.Abilities['..tostring(slotIndex)..'].CurrentLevel='..tostring(currentLevel))
				CheckAbilities()
			end
		end
	end

	for abilityIndex = Game.ABILITIES_START, Game.ABILITIES_END, 1 do
		local slotIndex = 1 + (abilityIndex - Game.ABILITIES_START)
		LHA.Abilities[slotIndex] = {}
		AltInfoRevisedGamePanel:RegisterWatch('ActiveInventoryDescription'..tostring(abilityIndex), function(...) ActiveAbilityDescription(slotIndex, ...) end)
		AltInfoRevisedGamePanel:RegisterWatch('ActiveInventoryStatus'     ..tostring(abilityIndex), function(...) ActiveAbilityStatus(slotIndex, ...)      end)
	end

	println('^555Debug: game_air.lua:InitActiveTarget() END')
end

local function GetEffectiveCreepHealth()
	local CreepHealthMelee, CreepHealthRange = 550 + (LHA.TimeFactor * 10), 300 + (LHA.TimeFactor * 10)

	LHA.EffectiveCreepHealthMeleeEnemy = CreepHealthMelee * GetArmorFactor(2)
	LHA.EffectiveCreepHealthRangeEnemy = CreepHealthRange * GetArmorFactor(0)
	LHA.EffectiveCreepHealthMeleeAlly  = CreepHealthMelee * GetArmorFactor(2)
	LHA.EffectiveCreepHealthRangeAlly  = CreepHealthRange * GetArmorFactor(0)

	println('^gLHA.EffectiveCreepHealthMeleeEnemy='..tostring(LHA.EffectiveCreepHealthMeleeEnemy))
	println('^gLHA.EffectiveCreepHealthRangeEnemy='..tostring(LHA.EffectiveCreepHealthRangeEnemy))
	println('^gLHA.EffectiveCreepHealthMeleeAlly ='..tostring(LHA.EffectiveCreepHealthMeleeAlly))
	println('^gLHA.EffectiveCreepHealthRangeAlly ='..tostring(LHA.EffectiveCreepHealthRangeAlly))

	Trigger('LHAUpdated')
end

local function InitMatchTime()
	println('^555Debug: game_air.lua:InitMatchTime() BEGIN')
	
	LHA.TimeFactor = 0
	
	local function MatchTime(sourceWidget, matchTime, isPreMatchPhase)
		local TimeFactor = math.floor(tonumber(matchTime) / 420000)
		if (LHA.TimeFactor ~= TimeFactor) then
			LHA.TimeFactor = TimeFactor
			println('^gLHA.TimeFactor=' .. tostring(LHA.TimeFactor))
			GetEffectiveCreepHealth()
		end
	end
	AltInfoRevisedGamePanel:RegisterWatch('MatchTime', MatchTime)
	
	println('^555Debug: game_air.lua:InitMatchTime() END')
end

function LHA:Initialize()
	println('^555Debug: LHA:Initialize() BEGIN')

	AltInfoRevisedGamePanel = interface:GetWidget('AltInfoRevisedGamePanel')

	InitActiveTarget()
	InitMatchTime()
	
	AltInfoRevisedGamePanel = nil
	InitActiveTarget = nil
	InitMatchTime = nil
		
	GetEffectiveCreepHealth() -- Initialize creep health values
	GetEffectiveDamage() -- Initialize effective damage values
	CheckItems() -- Initialize item state

	self.Initialize = nil

	println('^555Debug: LHA:Initialize() END')
end
